package com.tian.common.config;

import com.tian.common.utils.StringUtils;
import com.tian.common.utils.YamlUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

public class TianYaConfig {
    private static final Logger log =  LogManager.getLogger(TianYaConfig.class);

    private static String NAME = "application.yml";
    private static String PRO_NAME = "application-pro.yml";
    private static String DEV_NAME = "application-dev.yml";

    /**
     * 当前对象实例
     */
    private static TianYaConfig global;

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = new HashMap<String, String>();

    private TianYaConfig() {
    }

    /**
     * 静态工厂方法
     */
    public static synchronized TianYaConfig getInstance() {
        if (global == null) {
            global = new TianYaConfig();
        }
        return global;
    }

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            Map<?, ?> yamlMap = null;
            try {
                yamlMap = YamlUtil.loadYaml(NAME);
                value = String.valueOf(YamlUtil.getProperty(yamlMap, key));
                map.put(key, value != null ? value : StringUtils.EMPTY);
            } catch (FileNotFoundException e) {
                log.error("获取全局配置异常 {}", key);
            }
        }

        //如果没有找到key 去子配置文件中寻找
        if (value == null || "null".equals(value)) {
            Map<?, ?> yamlMap = null;
            try {
                String currentYml = getConfig("spring.profiles.active");

                if ("dev".equals(currentYml)) {
                    yamlMap = YamlUtil.loadYaml(DEV_NAME);
                } else if ("pro".equals(currentYml)) {
                    yamlMap = YamlUtil.loadYaml(PRO_NAME);
                }
                value = String.valueOf(YamlUtil.getProperty(yamlMap, key));
                map.put(key, value != null ? value : StringUtils.EMPTY);
            } catch (FileNotFoundException e) {
                log.error("获取全局配置异常 {}", key);
            }
        }

        return value;
    }

    /**
     * 获取项目名称
     */
    public static String getName() {
        return StringUtils.nvl(getConfig("tianya.name"), "grxm");
    }

    /**
     * 获取项目版本
     */
    public static String getVersion() {
        return StringUtils.nvl(getConfig("tianya.version"), "1.0.0");
    }

    /**
     * 获取版权年份
     */
    public static String getCopyrightYear() {
        return StringUtils.nvl(getConfig("tianya.copyrightYear"), "2021");
    }

    /**
     * 获取下载路径
     */
    public static String getDownloadPath() {
        return getConfig("tianya.profile") + "download/";
    }

    /**
     * 获取上传路径
     */
    public static String getUploadPath() {
        return getConfig("tianya.profile");
    }

    /**
     * 获取excel导出路径
     */
    public static String getExcelPath() {
        return getConfig("tianya.excelFile");
    }
}
